package com.chy.blog_server.modular.web.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_server.core.util.PageAndParamUtil;
import com.chy.blog_server.modular.web.entity.BoMsgTemplate;
import com.chy.blog_server.modular.web.service.IBoMsgTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：短信模板 controller
 * 作者：chenhuiyan
 * 时间：2024/08/10
 */
@Controller
@RequestMapping(value = "/web/boMsgTemplate")
public class BoMsgTemplateController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/boMsgTemplate/";

    @Autowired
    private IBoMsgTemplateService boMsgTemplateService;

    /**
     * 标首页
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "boMsgTemplate";
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boMsgTemplateService.list(params); //获取数据列表
            Integer count = boMsgTemplateService.getCount(params); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 跳转添加/编辑页面
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "boMsgTemplate_add";
    }

    /**
     * 编辑
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "boMsgTemplate_add";
    }

    /**
     * 保存
     * @param boMsgTemplate
     * @param request
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> save(BoMsgTemplate boMsgTemplate, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try {
            if(StrUtil.isEmpty(boMsgTemplate.getId())){
                boMsgTemplate.preInsert(request);
                boMsgTemplateService.save(boMsgTemplate);
                res.put("state", "200");
                res.put("message", "添加成功");
            }else{
                boMsgTemplate.preUpdate(request);
                boMsgTemplateService.updateById(boMsgTemplate);
                res.put("state", "200");
                res.put("message", "修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "添加失败");
        }
        return res;
    }

    /**
     * 选择短信签名
     * @param bookDirectoryId
     * @param bookDirectoryName
     * @return
     */
    @RequestMapping(value = "/chooseMsgSign")
    public String chooseBook(Model model, String bookDirectoryId, String bookDirectoryName){
        model.addAttribute("msgSignId", bookDirectoryId);
        model.addAttribute("msgSignName", bookDirectoryName);
        return PREFIX + "boMsgSignIndex";
    }

    /**
     * 查询详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfoById")
    @ResponseBody
    public Map<String, Object> getInfoById(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            res = boMsgTemplateService.getInfoById(id);
            res.put("state", "200");
            res.put("message", "获取详情成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取详情失败");
        }
        return res;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            BoMsgTemplate boMsgTemplate = boMsgTemplateService.getById(id);
            boMsgTemplate.setDelFlag(1);
            boMsgTemplateService.updateById(boMsgTemplate);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 回收站首页
     * @return
     */
    @RequestMapping(value = "/recycleIndex")
    public String recycleIndex(){
        return PREFIX + "boMsgTemplateRecycle";
    }

    /**
     * 查询已删除列表
     * @return
     */
    @RequestMapping(value = "/recycleList")
    @ResponseBody
    public Map<String, Object> recycleList(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boMsgTemplateService.recycleList(params); //获取数据列表
            Integer count = boMsgTemplateService.getBaseMapper().selectCount(new QueryWrapper<BoMsgTemplate>().eq("del_flag", 1)); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 彻底删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/thoroughlyDelete")
    @ResponseBody
    public Map<String, Object> thoroughlyDelete(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            int i = boMsgTemplateService.thoroughlyDelete(id);
            if(i != 0){
                res.put("state", "200");
                res.put("message", "删除成功");
            }else {
                res.put("state", "500");
                res.put("message", "删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 恢复
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/restore")
    @ResponseBody
    public Map<String, Object> restore(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoMsgTemplate boMsgTemplate = boMsgTemplateService.getById(id);
            boMsgTemplate.setDelFlag(0);
            boMsgTemplate.preUpdate(request);
            boMsgTemplateService.updateById(boMsgTemplate);
            res.put("state", "200");
            res.put("message", "恢复成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "恢复失败");
        }
        return res;
    }

}
